using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GanagroLiteBO;
using GLiteInterfaces;

namespace GanagroLite      
{
    public partial class FormOperaciones : WinPart, IOperacionView
    {
        protected NotaBO currentNota;

        public FormOperaciones()
        {
            InitializeComponent();
        }

        protected internal override object GetIdValue()
        {
            return "Form Operaciones";
        }

        public override string ToString()
        {
            return "Form Operaciones";
        }

        protected bool GuardarNota()
        {

            bool success = true;
            if (string.IsNullOrEmpty(currentNota.Nota.Trim()))
            {
                MessageBox.Show("Nota no puede ser vacia");
                return false;
            }

            if (currentNota.Isvalid() == false)
            {
                string msg = string.Format("{0} Haga click en OK para continuar o Cancel para salir", currentNota.Warnings());
                if (MessageBox.Show(msg, "Advertencia", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                { 
                    success = false; 
                }
            }

            if (success)
            {
                currentNota.Guardar();
                MainForm.Instance.StatusLabel1.Text = "La nota ha sido guardada";
            }

            return success;
        }

        internal void VGuardar<T>(IOperacionView IOV, BindingSource BS1, bool SuppressValidation) where T : IOperacion
        {
            Cursor cCursor = System.Windows.Forms.Cursor.Current;
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
            string ultimoCodigo = string.Empty;

            try
            {
                BS1.SuspendBinding();

                string errorMessages = string.Empty, infoMessages = string.Empty;

                if (GuardarNota() == false)
                {
                    return;
                }

                utilities.Guardar_View<T>((BindingListView<T>)BS1.DataSource, ref infoMessages, ref errorMessages, ref ultimoCodigo, SuppressValidation);

                if (errorMessages.Length > 0)
                {
                    MessageBox.Show(errorMessages, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

                IOV.status = infoMessages;

                BS1.ResumeBinding();

                ((BindingListView<T>)BS1.DataSource).Clear();

                IOV.status2 = string.Format("Ultimo codigo procesado: {0}", ultimoCodigo);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = cCursor;
            }

        }

        internal static bool confirmaSalir(Int32 numeroRegistros)
        {
            bool confirmar = true;

            if (numeroRegistros > 1)
            {
                confirmar = (MessageBox.Show("Esta seguro que desea salir?", "Confirmar", MessageBoxButtons.OKCancel) == DialogResult.OK);
            }

            return confirmar;
        }

        public void DisplayMessage(string message)
        {
            MessageBox.Show(message,"Advertencia",MessageBoxButtons.OK);
        }

        #region UI events

        private void FormOperaciones_Load(object sender, EventArgs e)
        {
            this.dtPickerFecha.Select();
        }


        #endregion

        #region IOperacionView Members

        public string status
        {
            get
            {
                return MainForm.Instance.StatusLabel1.Text;
            }
            set
            {
                MainForm.Instance.StatusLabel1.Text = value;
                this.Update();
            }
        }

        public string status2
        {
            get
            {
                return MainForm.Instance.StatusLabel2.Text;
            }
            set
            {
                MainForm.Instance.StatusLabel2.Text = value;
                this.Update();

            }
        }

        public void SetUIBusyStatus()
        {
            MainForm.Instance.Cursor = Cursors.WaitCursor;
            this.Update();
        }

        public void SetUIDefaultStatus()
        {
            MainForm.Instance.Cursor = Cursors.Default;
            this.Update();
        }

        #endregion
    }
}